package ru.urfc.portal.model;

import javax.persistence.Entity;
import javax.persistence.OneToOne;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

import ru.urfc.portal.ui.client.Constants;

@Entity
@XmlRootElement(name = Constants.NOTIFICATION_SETTINGS)
public class NotificationSettings extends SysObject {
	private boolean global;
	@OneToOne(mappedBy = "notificationSettings")
	private Client client;
	private boolean useGlobal;
	private boolean deviceCertificateExpiring;
	private boolean netLicenseExpiring;
	private int expirationNotifyInterval;
	private boolean notifyAboutUnpaidInvoices;
	private int invoiceUnpaidDays;
	private int invoiceUnpaidNotifyInterval;
	
	public NotificationSettings() {
	}

	public NotificationSettings(Client client, boolean useGlobal,
			boolean deviceCertificateExpiring, boolean netLicenseExpiring,
			int expirationNotifyInterval, boolean notifyAboutUnpaidInvoices,
			int invoiceUnpaidDays, int invoiceUnpaidNotifyInterval) {
		this.client = client;
		this.useGlobal = useGlobal;
		this.deviceCertificateExpiring = deviceCertificateExpiring;
		this.netLicenseExpiring = netLicenseExpiring;
		this.expirationNotifyInterval = expirationNotifyInterval;
		this.notifyAboutUnpaidInvoices = notifyAboutUnpaidInvoices;
		this.invoiceUnpaidDays = invoiceUnpaidDays;
		this.invoiceUnpaidNotifyInterval = invoiceUnpaidNotifyInterval;
	}
	
	public NotificationSettings(NotificationSettings globalSettings) {
		this (null, false, 
				globalSettings.deviceCertificateExpiring,
				globalSettings.netLicenseExpiring,
				globalSettings.expirationNotifyInterval,
				globalSettings.notifyAboutUnpaidInvoices,
				globalSettings.invoiceUnpaidDays, 
				globalSettings.invoiceUnpaidNotifyInterval);
	}

	@XmlTransient
	public boolean isGlobal() {
		return global;
	}

	@XmlTransient
	public Client getClient() {
		return client;
	}
	
	public void setGlobal(boolean global) {
		this.global = global;
	}

	@XmlElement
	public boolean isUseGlobal() {
		return useGlobal;
	}

	public void setUseGlobal(boolean useGlobal) {
		this.useGlobal = useGlobal;
	}
	
	@XmlElement
	public boolean isDeviceCertificateExpiring() {
		return deviceCertificateExpiring;
	}
	
	public void setDeviceCertificateExpiring(boolean deviceCertificateExpiring) {
		this.deviceCertificateExpiring = deviceCertificateExpiring;
	}

	@XmlElement
	public boolean isNetLicenseExpiring() {
		return netLicenseExpiring;
	}
	
	public void setNetLicenseExpiring(boolean netLicenseExpiring) {
		this.netLicenseExpiring = netLicenseExpiring;
	}

	@XmlElement
	public int getExpirationNotifyInterval() {
		return expirationNotifyInterval;
	}
	
	public void setExpirationNotifyInterval(int expirationNotifyInterval) {
		this.expirationNotifyInterval = expirationNotifyInterval;
	}
	
	@XmlElement
	public boolean isNotifyAboutUnpaidInvoices() {
		return notifyAboutUnpaidInvoices;
	}
	
	public void setNotifyAboutUnpaidInvoices(boolean notifyAboutUnpaidInvoices) {
		this.notifyAboutUnpaidInvoices = notifyAboutUnpaidInvoices;
	}

	@XmlElement
	public int getInvoiceUnpaidDays() {
		return invoiceUnpaidDays;
	}
	
	public void setInvoiceUnpaidDays(int invoiceUnpaidDays) {
		this.invoiceUnpaidDays = invoiceUnpaidDays;
	}

	@XmlElement
	public int getInvoiceUnpaidNotifyInterval() {
		return invoiceUnpaidNotifyInterval;
	}
	
	public void setInvoiceUnpaidNotifyInterval(int invoiceUnpaidNotifyInterval) {
		this.invoiceUnpaidNotifyInterval = invoiceUnpaidNotifyInterval;
	}

	public NotificationSettings updateAsGlobalSettings(NotificationSettings settings) {
		setDeviceCertificateExpiring(settings.isDeviceCertificateExpiring());
		setNetLicenseExpiring(settings.isNetLicenseExpiring());
		setExpirationNotifyInterval(settings.getExpirationNotifyInterval());
		setInvoiceUnpaidDays(settings.getInvoiceUnpaidDays());
		setInvoiceUnpaidNotifyInterval(settings.getInvoiceUnpaidNotifyInterval());
		return this;
	}

	public NotificationSettings updateAsClientSettings(NotificationSettings settings) {
		setUseGlobal(settings.isUseGlobal());
		setNotifyAboutUnpaidInvoices(settings.isNotifyAboutUnpaidInvoices());
		setInvoiceUnpaidDays(settings.getInvoiceUnpaidDays());
		setInvoiceUnpaidNotifyInterval(settings.getInvoiceUnpaidNotifyInterval());
		return this;
	}
	
}